package com.yx.cloud.activity;

import android.view.View;

import com.iot.product.sweeper.bean.CameraSettings;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.CameraSettingsActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.iot.product.sweeper.bean.Robot;
import com.iot.common.utils.Event;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class CameraSettingsActivity extends BaseActivity implements View.OnClickListener {
    private CameraSettingsActivityBinding binding;
    private Robot robot;
    private CameraSettings cameraSettings;

    @Override
    protected void onCreate() {
        binding = CameraSettingsActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {
        binding.ivCameraSettingsReturn.setOnClickListener(this);
        binding.llAIAvoidance.setOnClickListener(this);
        binding.llHomeEnvironment.setOnClickListener(this);
        binding.llLiveVideo.setOnClickListener(this);

        setInitialization();
    }

    private void setInitialization() {

        robot = AppContext.get().getCurrentDevice();
        cameraSettings = robot.getCameraSettings();

        if (cameraSettings.isAiAvoidanceSwitch()) {
            binding.tvAiThreeState.setTextColor(getResources().getColor(R.color.button_color1));
            binding.tvAiThreeState.setText(getString(R.string.U_Opended));
        } else {
            binding.tvAiThreeState.setTextColor(getResources().getColor(R.color.textview_color1));
            binding.tvAiThreeState.setText(getString(R.string.U_GO_Open));
        }
        if (cameraSettings.isHomeEnvironmentRecognitionSwitch()) {
            binding.tvHomeState.setTextColor(getResources().getColor(R.color.button_color1));
            binding.tvHomeState.setText(getString(R.string.U_Opended));
        } else {
            binding.tvHomeState.setTextColor(getResources().getColor(R.color.textview_color1));
            binding.tvHomeState.setText(getString(R.string.U_GO_Open));
        }
        if (cameraSettings.isLiveVideoSwitch()) {
            binding.tvRealTimeVideoState.setTextColor(getResources().getColor(R.color.button_color1));
            binding.tvRealTimeVideoState.setText(getString(R.string.U_Opended));
        } else {
            binding.tvRealTimeVideoState.setTextColor(getResources().getColor(R.color.textview_color1));
            binding.tvRealTimeVideoState.setText(getString(R.string.U_GO_Open));
        }
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_cameraSettingsReturn) {
            Intent_utils.backIntent(this);
        } else if (id == R.id.ll_AI_Avoidance) {
            Intent_utils.enterIntent(this, AIAvoidanceActivity.class);
        } else if (id == R.id.ll_home_Environment) {
            //家居环境
            Intent_utils.enterIntent(this, HomeEnvironmentRecognitionActivity.class);
        } else if (id == R.id.ll_live_Video) {
            Intent_utils.enterIntent(this, RealTimeVideoActivity.class);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_CAMERASETTING:
                setInitialization();
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
